﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Transportr
{
    public class MainViewModel : BaseItemViewModel
    {
        private string mFilterText;

        public SampleModel Sample { get; private set; }
        public BrowserModel Browser { get; private set; }
        public FavoriteLinesModel FavoriteLines { get; private set; }
        public LookaroundModel Near { get; private set; }
        public MapModel Map { get; private set; }

        public ObservableCollection<LineItemViewModel> FilteredBrowserItems
        {
            get
            {
                ObservableCollection<LineItemViewModel> o = new ObservableCollection<LineItemViewModel>();
                foreach (LineItemViewModel item in Browser.Items)
                {
                    if (FilterText == "" || item.LineNumber.Contains(FilterText))
                        o.Add(item);
                }
                return o;
            }
        }

        public string FilterText
        {
            get { return mFilterText; }
            set
            {
                if (value != mFilterText)
                {
                    mFilterText = value;
                    NotifyPropertyChanged("FilterText");
                    NotifyPropertyChanged("FilteredBrowserItems");
                }
            }
        }

        public MainViewModel()
        {
            mFilterText = "";
            FavoriteStore.Initialize();
            this.Sample = new SampleModel();
            this.Browser = new BrowserModel();
            this.FavoriteLines = new FavoriteLinesModel();
            this.Near = new LookaroundModel();
            this.Map = new MapModel();
#if WINDOWS_PHONE
            Browser.Items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(BrowserItems_CollectionChanged);
        }

        void BrowserItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            NotifyPropertyChanged("FilteredBrowserItems");
#endif
        }
    }
}